<?php

class  BeeDb{

	public $link;
	public $tablePrefix="";
	
	
	/**
	 * 转换表名称
	 * 	
	 */
	
	public function quoteTable($tableName){
		
		return $this->tablePrefix.$tableName;
		
	}
	
	/**
	 * 转意字符窜 防止sql injection 错误
	 *
	 * @param string $string
	 * @return string
	 */
	public function quote($string){
		 return mysql_escape_string($string);	
		
	}
	
	/**
	 * *
	 *
	 * @param string $tableName
	 * @param string $filter
	 * @param array $array
	 * @param array $pageMan
	 * @param array $dataList
	 */
	function buildPageMan($tableName,$filter,$order,$array,&$pageMan,&$dataList){
	
		
		$array["totalCounts"]=$this->numCount($tableName,$filter);		
		
		$pageMano=new BeePageMan($array);
		$pageMan=$pageMano->asArray();
		if ($array["totalCounts"]){
		$dataList=$this->getPageList($tableName,$filter,$order,$pageMan["currentPage"],$pageMan["perCount"]);	
		}
	
	}
	
/**
 * *
 *
 * 直接返回sql一行数据对应的结果
 * @param string $str
 * @return array
 */
	public function oneFieldArray($tableName,$str,$filter=false,$order=false,$page=1,$perCount=10){
		
		$limit=" limit ". (($page-1)*$perCount)." , $perCount";
		$sql=BeeDbUtil::getSimpleQueryStr($tableName,$filter,$str,$order).$limit;
		//echo $sql;
		$r= $this->_excute($sql);
		$q=array();
		while ($row=mysql_fetch_array($r) ){
			//echo "aa";
			$q[]=$row[$str];
		}
		
		return $q;
		
	}
	
	
	/**
 * *
 *
 * 直接返回sql一行数据对应的结果
 * 添加时间 2008 07 04
 * @param string $str
 * @return array
 */
	public function oneAscArray($tableName,$str1,$str2,$filter=false,$order=false,$page=1,$perCount=10){
		
		$limit=" limit ". (($page-1)*$perCount)." , $perCount";
		$sql=BeeDbUtil::getSimpleQueryStr($tableName,$filter,$str1." , ".$str2,$order).$limit;
		//echo $sql;
		$r= $this->_excute($sql);
		$q=array();
		while ($row=mysql_fetch_array($r) ){			//echo "aa";			
			$q[$row[$str1]]=$row[$str2];
		}
		
		return $q;
		
	}
	
		/**
 * *
 *
 * 直接返回sql一行数据对应的结果
 * 添加时间 2008 07 04
 * @param string $str
 * @return array
 */
	public function oneAscArraySqlPageList($sql,$str1,$str2,$page=1,$perCount=10){
		
		$limit=" limit ". (($page-1)*$perCount)." , $perCount";
		$sql.=$limit;
		//echo $sql;
		$r= $this->_excute($sql);
		$q=array();
		while ($row=mysql_fetch_array($r) ){			//echo "aa";			
			$q[$row[$str1]]=$row[$str2];
		}
		
		return $q;
		
	}
	
	
	
	public function query($strsql){
		return mysql_query($strsql,$this->link);		
	}
	
/**
 * *
 *
 * 直接返回sql对应的结果
 * @param string $str
 * @return array
 */
	public function row($str){
		return $this->rowArray($str);
		//TODO 添加对象的实现 

	}

/**
 * *
 *
 * 直接返回sql对应的结果
 * @param string $str
 * @return array
 */
	public  function rowArray($str){   
		global $beeErrorInfo; 
		$rs= $this->_excute($str);
		if(!$rs){
			//echo "have error .the sql is : $str the error is ".mysql_error();
			$beeErrorInfo.="have error .the sql is : $str the error is ".mysql_error();
		}
		$row=mysql_fetch_array($rs);
		return  $row;
	}


	public function result($tableName,$filter){
		//TODO 添加对象的实现 
		return $this->resultArray($tableName,$filter);

	}

/**
 * 和get类似 ,被get 取代
 *
 * @param string $tableName
 * @param string $filter
 * @return array list
 */
	public function resultArray($tableName,$filter){		
		
		return $this->get($tableName,$filter);


	}
	
/**
 * 返回满足条件个数
 *
 * @param string $tableName
 * @param string $filter
 * @return int
 */
	public function numCount($tableName,$filter=false){
		
		global $beeErrorInfo; 
		$filterstr=($filter)?(" where $filter "):"";	
		$strsql="select count(*) as totalCount from $tableName $filterstr ";
		
		
		$rs=$this->_excute($strsql);
		if (!$rs) {
			$beeErrorInfo.="have error .the sql is : $str the error is ".mysql_error();
			return "";
		}
		$row=mysql_fetch_array($rs);
		return $row["totalCount"];
		
	}
	
	
/**
 * 返回符合条件的列表
 *
 * @param string $tableName
 * @param string $filter
 * @return array list
 */
	public function get($tableName,$filter=false,$str=false,$order=false){
		$str=BeeDbUtil::getSimpleQueryStr($tableName,$filter,$str,$order)	;
		return $this->_executeArray($str);

	}
	
/**
 * 返回符合条件的列表
 *
 * @param string $tableName
 * @param string $filter
 * @return array list
 */
	public function getBind($tableName,$filter=false,$str=false){
		$str=BeeDbUtil::getSimpleQueryStr($tableName,$filter,$str)	;
		return $this->_executeArray($str);	
		
		

	}
	



	
	/**
	 * *
	 * 返回表里面的数据,根据表名称和过滤条件
	 *
	 * @param string $tableName
	 * @param string $filter
	 * @return array
	 */
public function getRow($tableName,$filter=false,$str=false,$order=false){
	$str=BeeDbUtil::getSimpleQueryStr($tableName,$filter,$str,$order)	;
	
	return $this->rowArray($str);
	
	
}
/**
 * 返回符合条件分段列表
 *
 * @param string $tableName
 * @param string $filter
 * @param string $page
 * @param int $perCount
 * @return array
 */
public function getPageList($tableName,$filter=false,$order=false,$page=1,$perCount=10,$str=false){
		$limit=" limit ". (($page-1)*$perCount)." , $perCount";
		$str=BeeDbUtil::getSimpleQueryStr($tableName,$filter,$str,$order).$limit;
		//echo $str;
		return $this->_executeArray($str);

	}
	
	
	
	/**
 * 返回符合条件分段列表
 *
 * @param string $tableName
 * @param string $filter
 * @param string $page
 * @param int $perCount
 * @return array
 */
public function getColumnPageList($tableName,$strColumn=false,$filter=false,$order=false,$page=1,$perCount=10){
		$limit=" limit ". (($page-1)*$perCount)." , $perCount";
		$str=BeeDbUtil::getSimpleQueryStr($tableName,$filter,$strColumn,$order).$limit;
		//echo $str;
		return $this->_executeArray($str);

	}
	
	/**
	 * 插入数据
	 *
	 * @param string $tableName
	 * @param array $row
	 * @param boolen $debug
	 * @return int 
	 */
	
	public function insert($tableName,$row,$debug=false){
		global $beeErrorInfo;
	    $str=BeeDbUtil::getInsertStr($tableName,$row)	;
		if($debug){echo $str;}
		$value= $this->_excute($str);

		if(!$value){
			$beeErrorInfo.="have error .the sql is : $str the error is ".mysql_error();
			return "";
		}
		return  $value;

	}


/**
	 * 更新数据列表
	 *
	 * @param string $tableName
	 * @param array $row
	 * @param  string $filter
	 * @param boolen $debug
	 * @return int 
	 */


	public function updateList($tableName,$row,$filter,$debug=false){
		global $beeErrorInfo;
		$str=BeeDbUtil::getUpdateStr($tableName,$row,$filter);
		if($debug){echo $str;}
		$value= $this->_excute($str);
		if(!$value){
			$beeErrorInfo.="have error .the sql is : $str the error is ".mysql_error();
		}

		return  $value;
		

	}
	
	
/**
	 * 更新数据
	 *
	 * @param string $tableName
	 * @param array $row
	 * @param  string $filter
	 * @param boolen $debug
	 * @return int 
	 */


	public function update($tableName,$row,$filter,$debug=false){
		global $beeErrorInfo; 
		$str=BeeDbUtil::getUpdateStr($tableName,$row,$filter)." limit 1 ";
		if($debug){echo $str;}
		$value= $this->_excute($str);
		if(!$value){
			$beeErrorInfo.="have error .the sql is : $str the error is ".mysql_error();
		}

		return  $value;
		

	}

/**
	 * 删除数据
	 *
	 * @param string $tableName	 * 
	 * @param  string $filter
	 * @param boolen $debug
	 * @return int 
	 */
	public function delete($tableName,$filter){
		global $beeErrorInfo; 
	   $filterstr=($filter)?(" where $filter "):"";
	   $str="delete from $tableName  $filterstr limit 1 ";
		$value= $this->_excute($str);
		//echo $str;
		if(!$value){
			$beeErrorInfo.="have error .the sql is : $str the error is ".mysql_error();
		}
		return  $value;

	}
	
/**
	 * 删除数据
	 *
	 * @param string $tableName	 * 
	 * @param  string $filter
	 * @param boolen $debug
	 * @return int 
	 */
	public function deleteList($tableName,$filter){
		global $beeErrorInfo; 
	   $filterstr=($filter)?(" where $filter "):"";
	   $str="delete from $tableName  $filterstr ";
		$value= $this->_excute($str);
		//echo $str;
		if(!$value){
			$beeErrorInfo.="have error .the sql is : $str the error is ".mysql_error();
		}
		


		return  mysql_affected_rows($this->link);

	}

	/**
	 * 执行命令
	 *
	 * @param string $tableName	 * 
	 * @param  string $filter
	 * @param boolen $debug
	 * @return int 
	 */
	public function _excute($strsql){		
		
		return mysql_query($strsql,$this->link);

	}


	
	
	public function _executeArray($strsql){
		global $beeErrorInfo;
			//echo $str."\n";
		$rs= $this->_excute($strsql);
		if(!$rs){
			$beeErrorInfo.="have error .the sql is : $str the error is ".mysql_error();
			return "";
		}

		$rowList=array();
		
		while ($row=mysql_fetch_assoc($rs)) {
			
			//var_dump($row);
			array_push($rowList,$row);
			//echo count($row);
			//var_dump(array_keys($row));
		}
		//var_dump($rowList);
		return $rowList;
		
		
		
	}
	
	/**
	 * 连接数据库
	 *
	 * @param string $host
	 * @param string $user
	 * @param string $password
	 * @param string $db
	 * @return unknown
	 */
	public function connect($host,$user,$password,$db){
		
		$this->link=mysql_connect($host,$user,$password);
		if($this->link){
			mysql_select_db($db);
			mysql_query("set names utf8");
		}
		return $this->link;

	}


/**
 * 关闭数据连接
 *
 */
	public function close(){
		if($this->link){
			mysql_close($this->link);

		}

	}


}



class BeeDbUtil{
	
	
	
	public static function getInArrayStr($field,$array){
		//if (count($array)<1) return "IN ('')";//危险
		return  " $field IN ( ".implode(",",$array)." ) ";  	
}
	
/**
 * *addslashes是否应该被踢出去?
 * 统一使用安全入口?
 *
 * @param unknown_type $tableName
 * @param unknown_type $row
 * @param unknown_type $filter
 * @return unknown
 */
public static function getUpdateStr($tableName,$row,$filter){
	
	$filterstr=($filter)?(" where $filter "):"";	
		
	$count=count($row);	
	$i=0;
	$str="update  $tableName set ";
	foreach ($row as $key=>$value){
		$i++;
		$addstr=($i==$count)?"":",";		
		$str.=$key."='".addslashes($value)."'$addstr";
	}
	
	$str.="$filterstr";
	return $str;
	
	
	
	
	
}

public static  function getSimpleQueryStr($tableName,$filter,$strColumn=false,$order=false){
	
	$filterstr=($filter)?(" where $filter "):"";
	$strColumns=($strColumn)?$strColumn:"*";
	$orderstr=($order)?(" order by $order "):"";
	$str="select $strColumns from $tableName ";
	$str.="$filterstr".$orderstr;
	//echo $str;
	return $str;	
	
}




/**
 * 使用implode 实现代替?
 * 
 *
 * @param string $tableName
 * @param string $row
 * @param string $addSlashes
 * @return string
 */

public static function getInsertStr($tableName,$row,$addSlashes=true){	
	$count=count($row);	
	$i=0;
	$str="insert into $tableName (";
	foreach ($row as $key=>$value){
		$i++;
		$addstr=($i==$count)?"":",";		
		$str.=$key.$addstr;		
		
	}
	$str.=") values(";
	$i=0;
	foreach ($row as $key=>$value){
		$i++;
		$addstr=($i==$count)?"":",";	
		if($addSlashes)	{
		$str.="'".addslashes($value)."'".$addstr;}
		else{
		$str.="'".$value."'".$addstr;
		}
		
	}
	$str.=")";	
	
	return $str;
	
	
}
	
	
}





class BeePageMan{

	public $totalCounts;
	public $pages;
	public $currentPage;
	public $perCount;
	public $startPage;
	public $endPage;
	public $maxPages;
	public $startRecord;

	
	
	

	function BeePageMan($array){
		$totalCounts=$array["totalCounts"];
		$currentPage=$array["currentPage"];
		$perCount=$array["perCount"];
		$maxPages=$array["maxPages"];
		
		$this->totalCounts=$totalCounts;
		$this->perCount=($perCount>0)?$perCount:1;
		$this->maxPages=$maxPages;
		$this->pages=(int)floor((($totalCounts-1)/$this->perCount))+1;
		$this->currentPage=($currentPage>1)?$currentPage:1;
		$this->currentPage=($this->currentPage<=$this->pages)?$this->currentPage:1;
		$tmpStartPage=$this->currentPage-(int)($this->maxPages/2);
		$tmpEndPage=$this->currentPage+(int)($this->maxPages/2);

		$this->startPage=($tmpStartPage>1)?$tmpStartPage:1;
		$this->endPage=($tmpEndPage<$this->pages)?$tmpEndPage:$this->pages;

		$this->startRecord=(($this->currentPage-1)*$this->perCount)+1;
		
	}


	//yes need a function that object to array
	function asArray(){
		$array=array();
		$array["totalCounts"]=$this->totalCounts;
		$array["pages"]=$this->pages;
		$array["currentPage"]=$this->currentPage;
		$array["perCount"]=$this->perCount;
		$array["startPage"]=$this->startPage;
		$array["endPage"]=$this->endPage;
		$array["maxPages"]=$this->maxPages;
		$array["startRecord"]=$this->startRecord;
		return $array;


	}


	//do not use again
	function pageIt(){
		$this->currentPage=($this->currentPage>1)?$this->currentPage:1;
		$this->currentPage=($this->currentPage<$this-pages)?$this->currentPage:$this->pages;

		$tmpStartPage=$this->currentPage-(int)($this->maxPages/2);
		$this->tmpEndPage=$this->currentPage+(int)($this->maxPages/2);

		$this->startPage=($tmpStartPage>1)?$tmpStartPage:1;
		$this->endPage=($this->tmpEndPage<$this->pages)?$this->tmpEndPage:$this->pages;


	}





}



class BeeArrayUtil{
	

public static function pairArray($array,$key,$value){
	$q=array();
	foreach ($array as $a) {
		$q[$a[$key]]=$a[$value];
	}
	return $q;	
}
	
	
}





?>